#include "kernel/ilog.h"
#include "dynlibwin.h"
#include "kernel/debug.h"

namespace rengine3d {

	CDynLibWin::CDynLibWin() {
	}

	CDynLibWin::~CDynLibWin() {
		this->Unload();
	}

	bool CDynLibWin::Load(const string_t& path) {
		if (m_hModule)
			return false; // Error!

		// Load library
		//const String sWindowsPath = cUrl.GetWindowsPath();
		//if (sWindowsPath.GetFormat() == String::ASCII)
		m_hModule = ::LoadLibraryExA(path.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
		//else
		//m_hModule = ::LoadLibraryExW(sWindowsPath.GetUnicode(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);

		// Return whether loading of the library was successful

		return ((m_hModule) ? true: false);
		//if (m_hModule) {
		//	return true;
		//} else {
		//	return false;
		//}
	}

	bool CDynLibWin::IsLoaded() const {
		return (m_hModule != nullptr);
	}

	bool CDynLibWin::Unload() {
		if (m_hModule != nullptr) {
			if (::FreeLibrary(m_hModule)) {
				m_hModule = nullptr;
				return true;
			}
		}
		return false;
	}

	void* CDynLibWin::GetSymbol(const string_t& symbol) const {
		return m_hModule ? ::GetProcAddress(m_hModule, symbol.c_str()) : nullptr;
	}

}
